package com.kingdee.eas.xr.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.tree.DefaultTreeModel;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.ctrl.common.util.StreamUtil;
import com.kingdee.bos.ctrl.swing.tree.DefaultKingdeeTreeNode;
import com.kingdee.bos.dao.xml.impl.MDLoader;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.eas.base.core.util.EmptyUtil;
import com.kingdee.eas.basedata.org.NewOrgViewFacadeFactory;
import com.kingdee.eas.basedata.org.OrgStructureInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.fm.common.FileUtils;
import com.kingdee.eas.fm.common.util.ReadFileInfoHelper;
import com.kingdee.eas.framework.bireport.util.SimpleTreeNode;
import com.kingdee.eas.util.ResourceBase;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.util.db.SQLUtils;

public class XrCommonFacadeControllerBean extends AbstractXrCommonFacadeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.xr.app.XrCommonFacadeControllerBean");
    protected Set _getAllParentOrgIds(Context ctx, String treeId, String orgUnitId)
    throws BOSException, EASBizException
{
    String orgLongNumber;
    String sql;
    Connection conn;
    PreparedStatement stmt;
    ResultSet rs;
    Set list;























/*  62*/        orgLongNumber = _getOrgLongNumber(ctx, treeId, orgUnitId);
/*  63*/        sql = "SELECT  FUnitId FROM T_ORG_Structure t  WHERE ( LEFT( ? , LEN(flongnumber))=flongnumber)";

/*  65*/        conn = null;
/*  66*/        stmt = null;
/*  67*/        rs = null;
/*  68*/        list = new HashSet();

/*  70*/        try
    {
        conn = getConnection(ctx);
          stmt = conn.prepareStatement(sql);
          stmt.setString(1, orgLongNumber);
          for(rs = stmt.executeQuery(); rs.next(); list.add(rs.getString(1)));
    }
       catch(SQLException ex)
    {
          throw new BOSException(ex);
    }
       SQLUtils.cleanup(rs, stmt, conn);
    SQLUtils.cleanup(rs, stmt, conn);
       return list;
}
public String _getOrgLongNumber(Context ctx, String treeId, String orgUnitId)
    throws BOSException, EASBizException
{
    String sql;
    Connection conn;
    PreparedStatement stmt;
    ResultSet rs;






/* 101*/        sql = "SELECT FLongNumber     FROM T_ORG_Structure S WHERE (FTreeId = ?) AND (FUnitId = ?)";

/* 103*/        conn = null;
/* 104*/        stmt = null;
/* 105*/        rs = null;

/* 107*/label0:
    {
        String s;
/* <-MISALIGNED-> */ /* 107*/            try
        {
/* <-MISALIGNED-> */ /* 107*/                conn = getConnection(ctx);
/* <-MISALIGNED-> */ /* 108*/                stmt = conn.prepareStatement(sql);
/* <-MISALIGNED-> */ /* 109*/                DbUtil.prepareVarcharParam(stmt, 1, treeId);
/* <-MISALIGNED-> */ /* 110*/                DbUtil.prepareVarcharParam(stmt, 2, orgUnitId);
/* <-MISALIGNED-> */ /* 112*/                rs = stmt.executeQuery();
/* <-MISALIGNED-> */ /* 113*/                if(!rs.next())
/* <-MISALIGNED-> */ /* 114*/                    break label0;
/* <-MISALIGNED-> */ /* 114*/                s = rs.getString(1);
        }
/* <-MISALIGNED-> */ /* 116*/            catch(SQLException ex)
        {
/* <-MISALIGNED-> */ /* 118*/                throw new BOSException(ex);
        }
/* <-MISALIGNED-> */ /* 120*/            SQLUtils.cleanup(rs, stmt, conn);
/* <-MISALIGNED-> */ /* 121*/            return s;
    }
/* <-MISALIGNED-> */ /* 120*/        SQLUtils.cleanup(rs, stmt, conn);
    Exception exception;
/* <-MISALIGNED-> */ /* 120*/        SQLUtils.cleanup(rs, stmt, conn);
/* <-MISALIGNED-> */ /* 122*/        return null;
}
protected Set _getChildrenOrgIds(Context ctx, String treeId, String orgUnitId)
    throws BOSException, EASBizException
{
    Set set;
    String sql;
    Connection conn;
    PreparedStatement stmt;
    ResultSet rs;
/* <-MISALIGNED-> */ /* 131*/        set = new HashSet();
/* <-MISALIGNED-> */ /* 136*/        sql = "SELECT S.FUnitId FROM T_ORG_Structure S INNER JOIN T_ORG_Structure P ON S.FParentID = P.FID WHERE (P.FUnitId = ?) AND(S.FTreeId = ?)";
/* <-MISALIGNED-> */ /* 139*/        conn = null;
/* <-MISALIGNED-> */ /* 140*/        stmt = null;
/* <-MISALIGNED-> */ /* 141*/        rs = null;
/* <-MISALIGNED-> */ /* 143*/        try
    {
/* <-MISALIGNED-> */ /* 143*/            conn = getConnection(ctx);
/* <-MISALIGNED-> */ /* 144*/            stmt = conn.prepareStatement(sql);
/* <-MISALIGNED-> */ /* 145*/            DbUtil.prepareVarcharParam(stmt, 1, orgUnitId);
/* <-MISALIGNED-> */ /* 146*/            DbUtil.prepareVarcharParam(stmt, 2, treeId);
/* <-MISALIGNED-> */ /* 148*/            for(rs = stmt.executeQuery(); rs.next(); set.add(rs.getString(1)));
    }
/* <-MISALIGNED-> */ /* 152*/        catch(SQLException ex)
    {
/* <-MISALIGNED-> */ /* 153*/            throw new BOSException(ex);
    }
/* <-MISALIGNED-> */ /* 155*/        SQLUtils.cleanup(rs, stmt, conn);
    Exception exception;
/* <-MISALIGNED-> */ /* 155*/        SQLUtils.cleanup(rs, stmt, conn);
/* <-MISALIGNED-> */ /* 158*/        return set;
}
protected String _getParentOrgId(Context ctx, Set ids, String treeId)
    throws BOSException, EASBizException
{
/* <-MISALIGNED-> */ /* 166*/        return null;
}
protected String _locateMetaSvr(Context ctx, String metaName)
    throws BOSException, EASBizException
{
/* <-MISALIGNED-> */ /* 171*/        int idx = metaName.lastIndexOf('.');
/* <-MISALIGNED-> */ /* 172*/        String ext = metaName.substring(idx);
/* <-MISALIGNED-> */ /* 173*/        metaName = metaName.substring(0, idx).replace('.', '/');
/* <-MISALIGNED-> */ /* 174*/        if(!metaName.startsWith("/"))
/* <-MISALIGNED-> */ /* 175*/            metaName = (new StringBuilder()).append("/").append(metaName).toString();
/* <-MISALIGNED-> */ /* 176*/        metaName = (new StringBuilder()).append(metaName).append(ext).toString();
/* <-MISALIGNED-> */ /* 177*/        URL classUrl = null;
/* <-MISALIGNED-> */ /* 178*/        classUrl = MDLoader.createInstance(null).cl.findResource(metaName);
/* <-MISALIGNED-> */ /* 179*/        if(classUrl != null)
/* <-MISALIGNED-> */ /* 181*/            try
        {
/* <-MISALIGNED-> */ /* 181*/                return (new StringBuilder()).append(classUrl.toString()).append("\n").append(ReadFileInfoHelper.getModifiedTimeAndSize(classUrl)).toString();
        }
/* <-MISALIGNED-> */ /* 182*/            catch(Exception e)
        {
/* <-MISALIGNED-> */ /* 183*/                return null;
        }
/* <-MISALIGNED-> */ /* 186*/        else
/* <-MISALIGNED-> */ /* 186*/            return null;
}
protected String _locateClassSvr(Context ctx, String className)
    throws BOSException, EASBizException
{
/* <-MISALIGNED-> */ /* 191*/        int idx = className.lastIndexOf('.');
/* <-MISALIGNED-> */ /* 192*/        String ext = className.substring(idx);
/* <-MISALIGNED-> */ /* 193*/        className = className.substring(0, idx).replace('.', '/');
/* <-MISALIGNED-> */ /* 194*/        if(!className.startsWith("/"))
/* <-MISALIGNED-> */ /* 195*/            className = (new StringBuilder()).append("/").append(className).toString();
/* <-MISALIGNED-> */ /* 196*/        className = (new StringBuilder()).append(className).append(ext).toString();
/* <-MISALIGNED-> */ /* 197*/        URL classUrl = null;
///* <-MISALIGNED-> */ /* 198*/        classUrl = com.java.lang.Class.getResource(className);
///* <-MISALIGNED-> */ /* 199*/        if(classUrl != null)
///* <-MISALIGNED-> */ /* 201*/            try
//        {
///* <-MISALIGNED-> */ /* 201*/                return (new StringBuilder()).append(classUrl.toString()).append("\n").append(ReadFileInfoHelper.getModifiedTimeAndSize(classUrl)).toString();
//        }
///* <-MISALIGNED-> */ /* 202*/            catch(Exception e)
//        {
///* <-MISALIGNED-> */ /* 203*/                return null;
//        }
///* <-MISALIGNED-> */ /* 206*/        else
/* <-MISALIGNED-> */ /* 206*/            return null;
}
protected boolean _afterDownLoadClassOrJarFile(Context ctx, String className)
    throws BOSException, EASBizException
{
/* <-MISALIGNED-> */ /* 211*/        String destPath = (new StringBuilder()).append(System.getProperty("EAS_WEBCLIENT")).append("/").toString();
/* <-MISALIGNED-> */ /* 212*/        FileUtils.delFile((new StringBuilder()).append(destPath).append(className).toString());
/* <-MISALIGNED-> */ /* 213*/        return true;
}
protected Map _beforeDownLoadClassOrJarFile(Context ctx, String className)
    throws BOSException, EASBizException
{
/* <-MISALIGNED-> */ /* 219*/        Map ret = new HashMap();
/* <-MISALIGNED-> */ /* 220*/        ret.put("isSucceed", Boolean.FALSE);
/* <-MISALIGNED-> */ /* 222*/        try
    {
/* <-MISALIGNED-> */ /* 222*/            String destPath = (new StringBuilder()).append(System.getProperty("EAS_WEBCLIENT")).append("/").toString();
/* <-MISALIGNED-> */ /* 223*/            String isAlready = saveClassOrJarFileSvr(className, destPath);
/* <-MISALIGNED-> */ /* 224*/            if("ok".equals(isAlready))
        {
/* <-MISALIGNED-> */ /* 225*/                ret.put("isSucceed", Boolean.TRUE);
/* <-MISALIGNED-> */ /* 226*/                ret.put("infomation", className);
        } else
        {
/* <-MISALIGNED-> */ /* 228*/                ret.put("infomation", isAlready);
        }
    }
/* <-MISALIGNED-> */ /* 230*/        catch(Exception e)
    {
/* <-MISALIGNED-> */ /* 231*/            ret.put("infomation", (new StringBuilder()).append(ResourceBase.getString("com.kingdee.eas.fm.common.COMMONAutoGenerateResource", "4_FMCommonFacadeControllerBean", ctx.getLocale())).append(e).toString());
    }
/* <-MISALIGNED-> */ /* 233*/        return ret;
}
private static String saveClassOrJarFileSvr(String className, String destPath)
    throws Exception
{
    java.io.InputStream in;
    java.io.OutputStream out;
/* <-MISALIGNED-> */ /* 245*/        int idx = className.lastIndexOf('.');
/* <-MISALIGNED-> */ /* 246*/        String ext = className.substring(idx);
/* <-MISALIGNED-> */ /* 247*/        className = className.substring(0, idx).replace('.', '/');
/* <-MISALIGNED-> */ /* 248*/        if(!className.startsWith("/"))
/* <-MISALIGNED-> */ /* 249*/            className = (new StringBuilder()).append("/").append(className).toString();
/* <-MISALIGNED-> */ /* 250*/        className = (new StringBuilder()).append(className).append(ext).toString();
/* <-MISALIGNED-> */ /* 251*/        URL classUrl = null;
///* <-MISALIGNED-> */ /* 252*/        classUrl = java/lang/Class.getResource(className);
/* <-MISALIGNED-> */ /* 253*/        in = null;
/* <-MISALIGNED-> */ /* 254*/        if(classUrl != null)
///* <-MISALIGNED-> */ /* 255*/            in = java/lang/Class.getResourceAsStream(className);
///* <-MISALIGNED-> */ /* 258*/        else
/* <-MISALIGNED-> */ /* 258*/            try
        {
/* <-MISALIGNED-> */ /* 258*/                in = new FileInputStream(new File(className));
        }
/* <-MISALIGNED-> */ /* 259*/            catch(Exception e) { }
/* <-MISALIGNED-> */ /* 262*/        if(in == null)
///* <-MISALIGNED-> */ /* 263*/            break MISSING_BLOCK_LABEL_208;
/* <-MISALIGNED-> */ /* 263*/        out = new FileOutputStream(new File(destPath, className.split("/")[className.split("/").length - 1]));
///* <-MISALIGNED-> */ /* 266*/        StreamUtil.copy(in, out, 32768);
///* <-MISALIGNED-> */ /* 268*/        StreamUtil.closeStream(in);
///* <-MISALIGNED-> */ /* 269*/        StreamUtil.closeStream(out);
///* <-MISALIGNED-> */ /* 270*/        break MISSING_BLOCK_LABEL_205;
    Exception exception;
///* <-MISALIGNED-> */ /* 268*/        exception;
/* <-MISALIGNED-> */ /* 268*/        StreamUtil.closeStream(in);
///* <-MISALIGNED-> */ /* 269*/        StreamUtil.closeStream(out);
///* <-MISALIGNED-> */ /* 270*/        throw exception;
///* <-MISALIGNED-> */ /* 271*/        return "ok";
/* <-MISALIGNED-> */ /* 273*/        return "not found";
}
protected Timestamp _getServerDate(Context ctx)
    throws BOSException
{
/* <-MISALIGNED-> */ /* 287*/        Calendar cal = Calendar.getInstance();
/* <-MISALIGNED-> */ /* 288*/        Timestamp ts = new Timestamp(cal.getTimeInMillis());
/* <-MISALIGNED-> */ /* 289*/        return ts;
}
protected void _saveColorScheme(Context ctx, Map colorScheme, String bosType)
    throws BOSException, EASBizException
{


/* 305*/        if(colorScheme == null)
/* 306*/            return;
/* 307*/        java.util.Map.Entry entry = null;
/* 308*/        String delSql = "delete from T_COM_ColorScheme where fbostype=?";
/* 309*/        String insertSql = "insert into T_COM_ColorScheme (FState, FColor, FBosType) values(?, ?, ?)";
/* 310*/        List paramsList = new ArrayList();

/* 312*/        try
    {
/* <-MISALIGNED-> */ /* 312*/            DbUtil.execute(ctx, delSql, new Object[] {
/* <-MISALIGNED-> */ /* 312*/                bosType
        });
/* <-MISALIGNED-> */ /* 313*/            Iterator iter = colorScheme.entrySet().iterator();
/* <-MISALIGNED-> */ /* 313*/            do
        {
/* <-MISALIGNED-> */ /* 313*/                if(!iter.hasNext())
/* <-MISALIGNED-> */ /* 314*/                    break;
/* <-MISALIGNED-> */ /* 314*/                entry = (java.util.Map.Entry)iter.next();
/* <-MISALIGNED-> */ /* 315*/                if(entry.getKey() != null)
/* <-MISALIGNED-> */ /* 318*/                    paramsList.add(((Object) (new Object[] {
/* <-MISALIGNED-> */ /* 318*/                        (Integer)entry.getKey(), (String)entry.getValue(), bosType
                })));
        } while(true);
/* <-MISALIGNED-> */ /* 320*/            DbUtil.executeBatch(ctx, insertSql, paramsList);
    }
/* <-MISALIGNED-> */ /* 321*/        catch(BOSException ex)
    {
/* <-MISALIGNED-> */ /* 322*/            throw ex;
    }
}
protected Map _loadColorScheme(Context ctx, String bosType)
    throws BOSException, EASBizException
{
/* 338*/        IRowSet rst = null;
/* 339*/        Integer _state = null;
/* 340*/        String _color = null;
/* 341*/        Map colorScheme = new HashMap();
/* 342*/        String querySql = "select FState, FColor from T_COM_ColorScheme where fbostype=?";

/* 344*/        try
    {
/* <-MISALIGNED-> */ /* 344*/            for(rst = DbUtil.executeQuery(ctx, querySql, new Object[] {
/* <-MISALIGNED-> */ /* 344*/    bosType
}); rst.next(); colorScheme.put(_state, _color))
        {
/* <-MISALIGNED-> */ /* 346*/                _state = new Integer(rst.getInt(1));
/* <-MISALIGNED-> */ /* 347*/                _color = rst.getString(2);
        }
    }
/* <-MISALIGNED-> */ /* 350*/        catch(BOSException e)
    {
/* <-MISALIGNED-> */ /* 351*/            throw e;
    }
/* <-MISALIGNED-> */ /* 352*/        catch(SQLException ex)
    {
/* <-MISALIGNED-> */ /* 353*/            throw new BOSException(ex);
    }
/* <-MISALIGNED-> */ /* 355*/        return colorScheme;
}
protected SimpleTreeNode _getCompanyTree(Context ctx, String treeID, OrgStructureInfo rootInfo, IMetaDataPK metaDataPK, IMetaDataPK actionPK)
    throws BOSException, EASBizException
{








/* 375*/        DefaultKingdeeTreeNode rootNode = new DefaultKingdeeTreeNode(rootInfo);
/* 376*/        javax.swing.tree.TreeModel treeModel = new DefaultTreeModel(rootNode);

/* 378*/        buildCompanyTree(ctx, rootNode, treeID, rootInfo, metaDataPK, actionPK);

/* 380*/        return SimpleTreeNode.parseFrom(treeModel);
}
private void buildCompanyTree(Context ctx, DefaultKingdeeTreeNode rootNode, String treeID, OrgStructureInfo rootInfo, IMetaDataPK metaDataPK, IMetaDataPK actionPK)
    throws EASBizException, BOSException
{















/* 400*/        List list = NewOrgViewFacadeFactory.getLocalInstance(ctx).getViewDirectChildNodesByUIPKAndActionPK("00000000-0000-0000-0000-0000000000024F2827FD", rootInfo, metaDataPK, actionPK);

/* 402*/        if(EmptyUtil.isEmpty(list))
/* 403*/            return;


/* 406*/        DefaultKingdeeTreeNode childNode[] = new DefaultKingdeeTreeNode[list.size()];
/* 407*/        for(int i = 0; i < list.size(); i++)
    {/* 408*/            OrgStructureInfo info = (OrgStructureInfo)list.get(i);

/* 410*/            if(rootInfo.getId().toString().equals(info.getId().toString()))
/* 411*/                continue;

/* 413*/            childNode[i] = new DefaultKingdeeTreeNode(info);
/* 414*/            rootNode.add(childNode[i]);

/* 416*/            if(!info.isIsLeaf())
/* 417*/                buildCompanyTree(ctx, childNode[i], treeID, info, metaDataPK, actionPK);
    }
}
}